package com.runvp.common.hex;


public class StringToHex {

    public static void main(String[] args) {
        System.out.println(convertHexToString("3434353335343443343934393530334330303030303030374443344334383539"));
        System.out.println(convertStringToHex("6"));
    }

    public static String convertStringToHex(String str){

        char[] chars = str.toCharArray();

        StringBuilder hex = new StringBuilder();
        for (char aChar : chars) {
            if(Character.isDigit(aChar)){
                hex.append(HexCalculator.calculate(aChar));
            }else {
                hex.append(Integer.toHexString((int) aChar));
            }
        }

        return hex.toString();
    }

    public  static String convertHexToString(String hex){

        StringBuilder sb = new StringBuilder();
        StringBuilder temp = new StringBuilder();

        //将字符串分割成逐个字节，例如：323537 -> 32 35 37
        for( int i=0; i<hex.length()-1; i+=2 ){

            //子串
            String output = hex.substring(i, (i + 2));
            //转化为十进制整型数
            int decimal = Integer.parseInt(output, 16);
            //转化为十进制整型数对应的字符
            sb.append((char)decimal);

            temp.append(decimal);
        }

        return sb.toString();
    }
}
